package com.wisewin.circle.service;

import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.dao.UserDAO;
import com.wisewin.circle.dao.UserFollowDAO;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.UserFollowDO;
import com.wisewin.circle.util.PageInfo;
import com.wisewin.circle.util.PageObjectUtil;
import com.wisewin.circle.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class UserFollowService {

    @Autowired
    private UserFollowDAO userFollowDAO;
    @Autowired
    private UserDAO userDAO;

    // 查询我的粉丝列表
    public ResultDTO queryUserFansList(QueryParamBO queryParamBO, PageUtil pageUtil){
        Map<String, Object> map = new HashMap<>();
        map.put("userId", queryParamBO.getUserId());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(userFollowDAO.queryUserFansCount(map), userFollowDAO.queryUserFansList(map), pageUtil);
        if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
            return ResultDTOBuilder.success(pageData);
        }
        return ResultDTOBuilder.success(pageData);
    }

    // 查询我的关注列表
    public ResultDTO queryUserFollowList(QueryParamBO queryParamBO, PageUtil pageUtil){
        Map<String, Object> map = new HashMap<>();
        map.put("userId", queryParamBO.getUserId());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(userFollowDAO.queryUserFollowCount(map), userFollowDAO.queryUserFollowList(map), pageUtil);
        if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
            return ResultDTOBuilder.success(pageData);
        }
        return ResultDTOBuilder.success(pageData);
    }

    // 取消关注
    public ResultDTO cancleUserFollow(UserFollowDO userFollowDO){
        if(userFollowDO.getFollowUserId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询是否关注
        UserFollowDO userFollowOld = userFollowDAO.queryUserFollow(userFollowDO.getUserId(), userFollowDO.getFollowUserId());
        if(userFollowOld == null){
            return ResultDTOBuilder.failure("1", "您还未关注过该用户", null);
        }
        // 取消关注
        userFollowDAO.delete(userFollowDO.getUserId(), userFollowDO.getFollowUserId());
        //减少用户关注数量
        userDAO.cancleFolowNum(userFollowDO.getUserId());
        // 减少粉丝数量
        userDAO.cancleFansNum(userFollowDO.getFollowUserId());
        return ResultDTOBuilder.success(userFollowDO);
    }

    // 添加关注
    public ResultDTO addUserFollow(UserFollowDO userFollowDO){
        if(userFollowDO.getFollowUserId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        if(userFollowDO.getUserId() == userFollowDO.getFollowUserId()){
            return ResultDTOBuilder.failure("1", "关注自己", null);
        }
        // 查询是否关注
        UserFollowDO userFollowOld = userFollowDAO.queryUserFollow(userFollowDO.getUserId(), userFollowDO.getFollowUserId());
        if(userFollowOld != null){
            return ResultDTOBuilder.failure("1", "您已经关注过该用户", null);
        }
        // 添加关注
        userFollowDAO.insert(userFollowDO);
        // 添加用户关注数量
        userDAO.updateFolowNum(userFollowDO.getUserId());
        // 添加粉丝数量
        userDAO.updateFansNum(userFollowDO.getFollowUserId());
        return ResultDTOBuilder.success(userFollowDO);
    }
}
